package com.province.platform.common.utils;

import com.province.platform.common.constants.Key;
import com.province.platform.common.enums.PlatformErrorCode;
import com.province.platform.common.exception.PlatformException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;

public class CommonHandler {
    public static class Job {
        public void init(Configuration configuration) {
            // 检查配置（必填）
            String url = configuration.getNecessaryValue(Key.URL, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.URI, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.LOCAL_PART, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.USERNAME, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.PASSWORD, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.SECRET_KEY, PlatformErrorCode.REQUIRED_VALUE);
            configuration.getNecessaryValue(Key.TIMEOUT, PlatformErrorCode.REQUIRED_VALUE);

            String ipAddress = StrUtils.getIpAddress(url);
            if (StringUtils.isBlank(ipAddress)) {
                throw PlatformException.asPlatformException(PlatformErrorCode.REQUIRED_VALUE, "配置的url有误, " +
                        "正确的配置方式是http://XXX.XXX.XXX.XXX:XXX/XXX. 例如: http://192.168.128.130:9001/gdCardZks");
            }

            // 测试是否能够与服务器进行连接
            NetUtils.testConnection(url);
        }
    }

    public static class Task {
        private static final Logger LOG = LoggerFactory.getLogger(Task.class);

        private String url;

        private String uri;

        private String username;

        private String password;

        private String localPart;

        private String secretKey;

        private int timeout;

        private Call call;

        public void init(Configuration configuration) {
            this.url = configuration.getString(Key.URL);
            this.uri = configuration.getString(Key.URI);
            this.localPart = configuration.getString(Key.LOCAL_PART);
            this.timeout = configuration.getInt(Key.TIMEOUT);

            // 测试是否能够与服务器进行连接
            NetUtils.testConnection(url);

            try {
                Call call = (Call) new Service().createCall();
                call.setTargetEndpointAddress(new URL(url));
                call.setOperationName(new QName(uri, localPart));
                call.setTimeout(timeout);
            } catch (ServiceException e) {
                String message = "服务创建异常";
                LOG.error(message);
                throw PlatformException.asPlatformException(PlatformErrorCode.SERVICE_ACCESS_ERROR, message, e);
            } catch (MalformedURLException e) {
                String message = String.format("[%s]不符合URL的语法规则", url);
                LOG.error(message);
                throw PlatformException.asPlatformException(PlatformErrorCode.SERVICE_ACCESS_ERROR, message, e);
            }
        }
    }
}
